package com.tintintech.sendmail.protocol;

import java.util.Map;

import com.tintintech.sendmail.util.Base64;
import com.tintintech.sendmail.util.MailBoxConstants;
public class MimeMessage extends Message {

	protected String charset;
	
	public MimeMessage(Map<String, String> mailBox)
	{
		this.fromname = mailBox.get(MailBoxConstants.FROM_NAME);
		this.fromemail = mailBox.get(MailBoxConstants.FROM_EMAIL);
		this.toname  = mailBox.get(MailBoxConstants.TO_NAME);
		this.toemail = mailBox.get(MailBoxConstants.TO_EMAIL);
		
		this.subject = mailBox.get(MailBoxConstants.SUBJECT);
		this.content = mailBox.get(MailBoxConstants.CONTENT);

		this.html = new Boolean(mailBox.get(MailBoxConstants.IS_HTML)).booleanValue();
		this.notification = new Boolean(mailBox.get(MailBoxConstants.IS_RECEIPT));
	}
	
	public MimeMessage(Map<String, String> mailBox, String charset)
	{
		this(mailBox);
		this.charset = charset;
	}

	public String getCharset()
	{
		if (this.charset == null)
		{
			this.charset = "utf-8";
		}
		return new String(this.charset).toUpperCase();
	}
	
	public String getMessageBody()
	{
		StringBuffer data = new StringBuffer();
		try
		{
			data.append("Date: " + this.getSendDate() + CRLF);
			data.append("To: \"=?" + this.getCharset() + "?B?" + Base64.encode(this.getToName().getBytes(this.getCharset())) + "?=\" <" + this.getToEmail() + ">" + CRLF);
			data.append("From: \"=?" + this.getCharset() + "?B?" + Base64.encode(this.getFromName().getBytes(this.getCharset())) + "?=\" <" + this.getFromEmail() + ">" + CRLF);
			data.append("Subject: =?" + this.getCharset() + "?B?" + Base64.encode(this.getSubject().getBytes(this.getCharset())) + "?=" + CRLF);
			data.append("MIME-Version: 1.0" + CRLF);
			data.append("Content-Transfer-Encoding: base64" + CRLF);
			if (this.getHtml()) {
				data.append("Content-Type: text/html; charset=" + this.getCharset() + CRLF);
				data.append("Content-Disposition: inline" + CRLF);
			} else {
				data.append("Content-Type: text/plain; charset=" + this.getCharset() + CRLF);
			}
			data.append(CRLF);
			
			String base64Content = Base64.encode(this.getContent().getBytes(this.getCharset()));
			int i = 0, length = base64Content.length() - 1;
			StringBuffer content = new StringBuffer();
			for (;i < length;)
			{
				content.append(base64Content.charAt(i));
				if (i % 75 == 0 && i != 0)
				{
					
					content.append("\n");
				}
				i++;
			}
			data.append(content + CRLF);
		}
		catch (Exception e)
		{
			
		}
		
		return data.toString();
	}
	
	public String getMailBoxBody(String toname, String toemail, String fromname, String fromemail, String subject, String content)
	{
		String mail = "";
		try {
			System.out.println(subject.getBytes(this.getCharset()));
	        mail += "To: \"=?" + this.getCharset() + "?B?" + Base64.encode(toname.getBytes(this.getCharset())) + "?=\" <" + toemail + ">\r\n";
	        mail += "From: \"=?" + this.getCharset() + "?B?" + Base64.encode(fromname.getBytes(this.getCharset())) + "?=\" <" + fromemail + ">\r\n";
	        mail += "Subject: =?" + this.getCharset() + "?B?" + Base64.encode(subject.getBytes(this.getCharset())) + "?=\r\n";
	        mail += "MIME-Version: 1.0\r\n";
	        mail += "Content-Transfer-Encoding: base64\r\n";
	        mail += "Content-Type: text/html; charset=" + this.getCharset() + "\r\n";
	        mail += "Content-Disposition: inline\r\n";
	        mail += "\r\n";
			String base64Content = Base64.encode(this.getContent().getBytes(this.getCharset()));
			int i = 0, length = base64Content.length() - 1;
			StringBuffer body = new StringBuffer();
			for (;i < length;)
			{
				body.append(base64Content.charAt(i));
				if (i % 75 == 0 && i != 0)
				{
					
					body.append("\n");
				}
				i++;
			}
			mail += body + "\r\n";
			
		} catch (Exception e) {}
		System.out.println("body: " + mail);
		return mail;
	}
	
	
}
